
#import "JFTableViewLoader.h"
#import "JFTableVectorConfiguration.h"
/* 
 *	JFTableViewLoader, NeXT Systems Engineering
 *	Written by Joe Freeman
 *
 *	A list of these configuration objects is fed to a JFTableViewLoader
 *	to tell it what ivars and titles map into a DBTableView
 */


@implementation JFTableViewLoader

/* static char VERSION[] = " @(#) Version: 2, Project: TableViewLoader, Written By: Joe Freeman, 1992"; */

- (const char *)getInspectorClassName
{
    return "JFDBTVLoaderInspector";
}

- setTableView:anObject;
{
    tableView = anObject;
    [tableView setDataSource:self];
    return self;
}


- setDataList:anObject
{
    dataList = anObject;
    [tableView reloadData:self];

    return self;
}


/* this doesn't do what we want in the inspector in IB
 * because it relies on the tableView nstance var being set up
 * in ib, the connections in build mode, are fake.  So,
 * we have to have this method happen in awake
 */
- setConfigurationList:cList
{

    int                 i;
    int                 tCount;
    id                 <DBTableVectors > aVector;

    [[tableView window] disableFlushWindow];	/* DBTV ignores autodisplay */
    configList = cList;
 /* configure the columns */
 /* first figure out how many columns there are, and how many we need */
    tCount = [tableView columnCount];

    for (i = [configList count]; i < tCount; i++)
	[tableView removeColumnAt:[configList count]];

 /* then make the numbers match and load up the titles and identifiers */
    for (i = 0; i < [configList count]; i++) {
	if (i < tCount) {
	    aVector = [tableView columnAt:i];
	    [aVector setIdentifier:(id)i];
	    [aVector setTitle:[[configList objectAt:i] title]];
	} else {
	    [tableView addColumn:(id)i
	     withTitle:[[configList objectAt:i] title]];
	}
    }

    [[tableView window] reenableFlushWindow];
    [[tableView window] flushWindowIfNeeded];

    return self;
}

- setDataList:anObject andConfigurationList:cList
{
    [self setConfigurationList:cList];
 /* now load em up */
    [self setDataList:anObject];
    return self;
}

/*======================================================================
 *	Inspector support
 *======================================================================*/

- (DBTableView *) tableView
{
    return tableView;
}

- (List *) dataList
{
    if (dataList)
	return dataList;
    else
	return[[List alloc] init];
}

- (List *) configList
{
    if (configList)
	return configList;
    else
	return[[List alloc] init];
}

/*======================================================================
 *	Archiving
 *======================================================================*/

- awakeFromNib
{
    [self setConfigurationList:configList];
    return self;
}

- read:(NXTypedStream *) typedStream
{
    [super read:typedStream];

    dataList = NXReadObject(typedStream);
    configList = NXReadObject(typedStream);
    tableView = NXReadObject(typedStream);

    return self;
}

- write:(NXTypedStream *) typedStream
{
    [super write:typedStream];

    NXWriteObjectReference(typedStream, dataList);
    NXWriteObject(typedStream, configList);
    NXWriteObjectReference(typedStream, tableView);
    return self;
}

/*======================================================================
 *	DBTableView data source (psuedo-delegate) methods
 *======================================================================*/

- (unsigned int)rowCount
{
    return[dataList count];
}

/* DBTV sends this message when it needs to know what to display */
- getValueFor:identifier at:(unsigned int)aPosition into:aValue
{
 /*
  * note that when loaded, we set the identifiers to be the locations of the
  * configuration objects in the configuration list 
  */
    if (configList)
	[[configList objectAt:(int)identifier]
	 getValueFromObject:[dataList objectAt:aPosition]
	 into:aValue];
    else
	[aValue setStringValue:""];
    return nil;
}

/* DBTV sends this message when data in a cell has changed */
- setValueFor:identifier at:(unsigned int)aPosition from:aValue
{
    [[configList objectAt:(int)identifier]
     setValueForObject:[dataList objectAt:aPosition]
     from:aValue];
    return nil;
}

@end
